package com.ruoyi.manage.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.ListUtil;
import com.ruoyi.manage.organization.domain.OcBusinessFinancingOrder;
import com.ruoyi.manage.organization.domain.OcOrderCredit;
import com.ruoyi.manage.organization.mapper.OcOrderCreditMapper;
import com.ruoyi.manage.organization.service.IOcBusinessFinancingOrderService;
import com.ruoyi.manage.organization.service.IOcOrderCreditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 订单授信信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-19
 */
@Service
public class OcOrderCreditServiceImpl extends ServiceImpl<OcOrderCreditMapper, OcOrderCredit> implements IOcOrderCreditService {


    @Autowired
    IOcBusinessFinancingOrderService iOcBusinessFinancingOrderService;

    @Override
    public OcOrderCredit queryGetById(Long id) {
        OcOrderCredit ocOrderCredit = this.getById(id);
        return ocOrderCredit;
    }

    /**
     * 新增订单授信信息
     *
     * @param ocOrderCredit 订单授信信息
     * @return 结果
     */
    @Override
    public boolean addOcOrderCredit(OcOrderCredit ocOrderCredit) {
        boolean save = this.save(ocOrderCredit);
        if ("1".equals(ocOrderCredit.getCreditState())) {
            iOcBusinessFinancingOrderService.update(new UpdateWrapper<OcBusinessFinancingOrder>()
                    .set("credit_amount", ocOrderCredit.getCreditAmount())
                    .set("credit_time", ocOrderCredit.getCreditTime())
                    .set("order_state", "4")
                    .eq("order_no", ocOrderCredit.getOrderNo()));
            return true;
        }
        iOcBusinessFinancingOrderService.update(new UpdateWrapper<OcBusinessFinancingOrder>()
                .set("order_state", "3")
                .eq("order_no", ocOrderCredit.getOrderNo()));
        return save;
    }

    /**
     * 修改订单授信信息
     *
     * @param ocOrderCredit 订单授信信息
     * @return 结果
     */
    @Override
    public boolean updateOcOrderCredit(OcOrderCredit ocOrderCredit) {
        return this.updateById(ocOrderCredit);
    }

    /**
     * 批量删除订单授信信息
     *
     * @param ids 需要删除的订单授信信息主键
     * @return 结果
     */
    @Override
    public boolean deleteOcOrderCreditByIds(Long[] ids) {
        return this.removeByIds(ListUtil.toList(ids));
    }

}
